package com.zd.web.servlet;

import java.io.IOException;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zd.entity.Chapter;
import com.zd.entity.Novel;
import com.zd.entity.PageBean;
import com.zd.service.AdminChapterService;
import com.zd.service.impl.AdminChapterServiceImpl;

/**
 * Servlet implementation class AdminChapterServlet
 */
@WebServlet("/AdminChapterServlet")
public class AdminChapterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	AdminChapterService nl=new AdminChapterServiceImpl();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		response.getWriter().append("Served at: ").append(request.getContextPath());
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		
		System.out.println("进入");
		//分页一波
		int aPageNum=5;
		String num=request.getParameter("nowPage");
		int nowPage=Integer.parseInt(num==null?"1":num);

		PageBean<Chapter> pageBean=nl.selectChapter(aPageNum,nowPage);
//		System.out.println(pageBean);
		request.setAttribute("pageBean", pageBean);
		request.getRequestDispatcher("chapter.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		doGet(request, response);
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		int chapterId=Integer.parseInt(request.getParameter("chapterId"));
		System.out.println(chapterId);
		//String novelId=request.getParameter("novelId");
		String chapterName=request.getParameter("chapterName");
		String text=request.getParameter("chapterContent");
		String updateTime=request.getParameter("updateTime");
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        LocalDate localDate = LocalDate.parse(updateTime, formatter);
        // 如果你需要java.util.Date对象
        Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
		Chapter chapter =new Chapter();
		chapter.setChapterContent(text);
		chapter.setChapterId(chapterId);
		chapter.setChapterName(chapterName);
		chapter.setUpdateTime(date);
		int num =nl.updateChapter(chapter);
		response.sendRedirect("chapter.jsp");
	}

}
